package cn.com.inossem.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Environment;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.netease.nim.uikit.ImageLoaderKit;
import com.netease.nim.uikit.NimUIKit;
import com.netease.nim.uikit.cache.FriendDataCache;
import com.netease.nim.uikit.cache.NimUserInfoCache;
import com.netease.nim.uikit.cache.TeamDataCache;
import com.netease.nim.uikit.contact.ContactProvider;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.NimStrings;
import com.netease.nimlib.sdk.SDKOptions;
import com.netease.nimlib.sdk.StatusBarNotificationConfig;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.uinfo.UserInfoProvider;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import cn.com.inossem.utils.DemoCache;
import cn.com.inossem.utils.wangyi.AppCrashHandler;
import cn.com.inossem.utils.wangyi.ExtraOptions;
import cn.com.inossem.utils.wangyi.Preferences;
import cn.com.inossem.utils.wangyi.SystemUtil;

public class NimApplication extends MultiDexApplication {
	private static Context ctx;
	public static String MANUFACTURER;

	public void onCreate() {
		super.onCreate();
		ctx = getApplicationContext();
		DemoCache.setContext(this);

		NIMClient.init(this, getLoginInfo(), options());

		ExtraOptions.provide();

		// crash handler
		AppCrashHandler.getInstance(this);
		if (inMainProcess()) {
			// init pinyin
			// PinYin.init(this);
			// PinYin.validate();

			// 初始化UIKit模块
			initUIKit();

			// 初始化消息提醒
			// NIMClient.toggleNotification(UserPreferences
			// .getNotificationToggle());

			// 注册网络通话来电
			// enableAVChat();

			// 注册白板会话
			// enableRTS();

			// 注册语言变化监听
			// registerLocaleReceiver(true);
		}
		Field[] fields = Build.class.getDeclaredFields();
		for (Field field : fields) {
			if (field.getName().equals("MANUFACTURER")) {
				try {
					MANUFACTURER = field.get(null).toString();
					break;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	private LoginInfo getLoginInfo() {
		String account = Preferences.getUserAccount();
		String token = Preferences.getUserToken();

		System.out.println("account" + account + "token" + token);

		if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(token)) {
			DemoCache.setAccount(account.toLowerCase());
			return new LoginInfo(account, token);
		} else {
			return null;
		}
	}

	// 如果返回值为 null，则全部使用默认参数。
	private SDKOptions options() {
		SDKOptions options = new SDKOptions();

		// 如果将新消息通知提醒托管给 SDK 完成，需要添加以下配置。否则无需设置。
		StatusBarNotificationConfig config = new StatusBarNotificationConfig();
		config.notificationEntrance = MainActivity.class; // 点击通知栏跳转到该Activity
		config.notificationSmallIconId = R.drawable.touxiang;
		options.statusBarNotificationConfig = config;

		// 配置保存图片，文件，log 等数据的目录
		// 如果 options 中没有设置这个值，SDK 会使用下面代码示例中的位置作为 SDK 的数据目录。
		// 该目录目前包含 log, file, image, audio, video, thumb 这6个目录。
		// 如果第三方 APP 需要缓存清理功能， 清理这个目录下面个子目录的内容即可。
		String sdkPath = Environment.getExternalStorageDirectory() + "/"
				+ getPackageName() + "/nim";
		options.sdkStorageRootPath = sdkPath;

		// 配置是否需要预下载附件缩略图，默认为 true
		options.preloadAttach = true;

		// 配置附件缩略图的尺寸大小，该值一般应根据屏幕尺寸来确定， 默认值为 Screen.width / 2
		options.thumbnailSize = 800 / 2;

		// 用户资料提供者, 目前主要用于提供用户资料，用于新消息通知栏中显示消息来源的头像和昵称
		options.userInfoProvider = new UserInfoProvider() {
			@Override
			public UserInfo getUserInfo(String account) {
				return null;
			}

			@Override
			public int getDefaultIconResId() {
				return R.drawable.touxiang;
			}

			@Override
			public Bitmap getTeamIcon(String tid) {
				return null;
			}

			@Override
			public Bitmap getAvatarForMessageNotifier(String account) {
				return null;
			}

			@Override
			public String getDisplayNameForMessageNotifier(String account,
			                                               String sessionId, SessionTypeEnum sessionType) {
				return null;
			}
		};
		return options;
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
	}

	public boolean inMainProcess() {
		String packageName = getPackageName();
		String processName = SystemUtil.getProcessName(this);
		return packageName.equals(processName);
	}

	/**
	 * 音视频通话配置与监听
	 */
	// private void enableAVChat() {
	// setupAVChat();
	// registerAVChatIncomingCallObserver(true);
	// }

	// private void setupAVChat() {
	// AVChatRingerConfig config = new AVChatRingerConfig();
	// config.res_connecting = R.raw.avchat_connecting;
	// config.res_no_response = R.raw.avchat_no_response;
	// config.res_peer_busy = R.raw.avchat_peer_busy;
	// config.res_peer_reject = R.raw.avchat_peer_reject;
	// config.res_ring = R.raw.avchat_ring;
	// AVChatManager.getInstance().setRingerConfig(config); // 设置铃声配置
	// }

	// private void registerAVChatIncomingCallObserver(boolean register) {
	// AVChatManager.getInstance().observeIncomingCall(new
	// Observer<AVChatData>() {
	// @Override
	// public void onEvent(AVChatData data) {
	// // 有网络来电打开AVChatActivity
	// AVChatProfile.getInstance().setAVChatting(true);
	// AVChatActivity.launch(DemoCache.getContext(), data,
	// AVChatActivity.FROM_BROADCASTRECEIVER);
	// }
	// }, register);
	// }

	/**
	 * 白板实时时会话配置与监听
	 */
	private void enableRTS() {
		// setupRTS();
		// registerRTSIncomingObserver(true);
	}

	// private void setupRTS() {
	// RTSRingerConfig config = new RTSRingerConfig();
	// config.res_connecting = R.raw.avchat_connecting;
	// config.res_no_response = R.raw.avchat_no_response;
	// config.res_peer_busy = R.raw.avchat_peer_busy;
	// config.res_peer_reject = R.raw.avchat_peer_reject;
	// config.res_ring = R.raw.avchat_ring;
	// RTSManager.getInstance().setRingerConfig(config); // 设置铃声配置
	// }

	// private void registerRTSIncomingObserver(boolean register) {
	// RTSManager.getInstance().observeIncomingSession(new Observer<RTSData>() {
	// @Override
	// public void onEvent(RTSData rtsData) {
	// RTSActivity.incomingSession(DemoCache.getContext(), rtsData,
	// RTSActivity.FROM_BROADCAST_RECEIVER);
	// }
	// }, register);
	// }

	private void registerLocaleReceiver(boolean register) {
		if (register) {
			updateLocale();
			IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
			registerReceiver(localeReceiver, filter);
		} else {
			unregisterReceiver(localeReceiver);
		}
	}

	private BroadcastReceiver localeReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(Intent.ACTION_LOCALE_CHANGED)) {
				updateLocale();
			}
		}
	};

	private void updateLocale() {
		NimStrings strings = new NimStrings();
		strings.status_bar_multi_messages_incoming = getString(R.string.nim_status_bar_multi_messages_incoming);
		strings.status_bar_image_message = getString(R.string.nim_status_bar_image_message);
		strings.status_bar_audio_message = getString(R.string.nim_status_bar_audio_message);
		strings.status_bar_custom_message = getString(R.string.nim_status_bar_custom_message);
		strings.status_bar_file_message = getString(R.string.nim_status_bar_file_message);
		strings.status_bar_location_message = getString(R.string.nim_status_bar_location_message);
		strings.status_bar_notification_message = getString(R.string.nim_status_bar_notification_message);
		strings.status_bar_ticker_text = getString(R.string.nim_status_bar_ticker_text);
		strings.status_bar_unsupported_message = getString(R.string.nim_status_bar_unsupported_message);
		strings.status_bar_video_message = getString(R.string.nim_status_bar_video_message);
		strings.status_bar_hidden_message_content = getString(R.string.nim_status_bar_hidden_msg_content);
		NIMClient.updateStrings(strings);
	}

	private void initUIKit() {
		// 初始化，需要传入用户信息提供者
		NimUIKit.init(this, infoProvider, contactProvider);

		// 设置地理位置提供者。如果需要发送地理位置消息，该参数必须提供。如果不需要，可以忽略。
		// NimUIKit.setLocationProvider(new NimDemoLocationProvider());

		// 会话窗口的定制初始化。
		// SessionHelper.init();

		// 通讯录列表定制初始化
		// ContactHelper.init();
	}

	private UserInfoProvider infoProvider = new UserInfoProvider() {
		@Override
		public UserInfo getUserInfo(String account) {
			UserInfo user = NimUserInfoCache.getInstance().getUserInfo(account);
			if (user == null) {
				NimUserInfoCache.getInstance().getUserInfoFromRemote(account,
						null);
			}

			return user;
		}

		@Override
		public int getDefaultIconResId() {
			return R.drawable.touxiang;
		}

		@Override
		public Bitmap getTeamIcon(String teamId) {
			Drawable drawable = getResources().getDrawable(
					R.drawable.nim_avatar_group);
			if (drawable instanceof BitmapDrawable) {
				return ((BitmapDrawable) drawable).getBitmap();
			}

			return null;
		}

		@Override
		public Bitmap getAvatarForMessageNotifier(String account) {
			UserInfo user = getUserInfo(account);
			if (user != null && !TextUtils.isEmpty(user.getAvatar())) {
				return ImageLoaderKit.getBitmapFromCache(user.getAvatar(),
						R.dimen.avatar_size_default,
						R.dimen.avatar_size_default);
			}

			return null;
		}

		@Override
		public String getDisplayNameForMessageNotifier(String account,
		                                               String sessionId, SessionTypeEnum sessionType) {
			String nick = null;
			if (sessionType == SessionTypeEnum.P2P) {
				nick = NimUserInfoCache.getInstance().getAlias(account);
			} else if (sessionType == SessionTypeEnum.Team) {
				nick = TeamDataCache.getInstance().getTeamNick(sessionId,
						account);
				if (TextUtils.isEmpty(nick)) {
					nick = NimUserInfoCache.getInstance().getAlias(account);
				}
			}
			// 返回null，交给sdk处理。如果对方有设置nick，sdk会显示nick
			if (TextUtils.isEmpty(nick)) {
				return null;
			}

			return nick;
		}
	};

	private ContactProvider contactProvider = new ContactProvider() {
		@Override
		public List<UserInfoProvider.UserInfo> getUserInfoOfMyFriends() {
			List<NimUserInfo> nimUsers = NimUserInfoCache.getInstance()
					.getAllUsersOfMyFriend();
			List<UserInfoProvider.UserInfo> users = new ArrayList<>(
					nimUsers.size());
			if (!nimUsers.isEmpty()) {
				users.addAll(nimUsers);
			}

			return users;
		}

		@Override
		public int getMyFriendsCount() {
			return FriendDataCache.getInstance().getMyFriendCounts();
		}

		@Override
		public String getUserDisplayName(String account) {
			return NimUserInfoCache.getInstance().getUserDisplayName(account);
		}
	};
}
